<?php

namespace app\api\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Exception;
use app\common\model\stock\Stock as StockModel;
use app\common\define\StockDefine;

/**
 * 获取股票基础信息
 *
 * Class Gupiaobase
 * @package app\api\command
 */
class StockBase extends Command{

    protected function configure(){
        $this->setName('StockBase')->setDescription('获取股票基础信息');
    }

    protected function execute(Input $input, Output $output){
        $token = StockDefine::STOCK_API_TOKEN;
        $requestUrl = "https://stockapi.com.cn/v1/base/all?token={$token}";
        $result = get_curl_data($requestUrl);
        $data = $result[1];
        $data = json_decode($data, true);
        if (!empty($data['data'])) {
            $list = $data['data'];
            foreach ($list as $item) {
                //if (substr($item['api_code'], 0, 2) == '30') {
                $type = (substr($item['api_code'], 0, 2) == '30') ? 300 : 100;
                $data = [
                    'name' => $item['name'],
                    'code' => $item['api_code'],
                    'type' => $type,
                    'jys' => $item['jys'],
                ];
                $re = StockModel::insert($data);
                echo $item['name'].'处理结果为'.$re."\n\r";
                //}
            }
        }
    }
}
